package com.clay.singleton.type1;

/**
 * @Author : clay
 * @Description:
 * @Date: Create in 20:15 2021/3/14
 * @Modified By:
 */
public class singletonTest1 {

    public static void main(String[] args) {

        Singleton instance = Singleton.getInstance();
        Singleton instance1 = Singleton.getInstance();

        System.out.println(instance==instance1);//同一个静态实例
        System.out.println(instance.hashCode());
        System.out.println(instance1.hashCode());

    }

}

//饿汉式(静态变量)
class Singleton
{

    //    1.构造器私有化

    private Singleton()
    {

    }

    //2.类的内部创建对象
    private final static Singleton instance=new Singleton();
//3.向外暴露一个静态的公共方法

    public static Singleton getInstance()
    {
        return instance;
    }

//4.代码实现
}
